
import md5Libs from "@/template/uview-ui/libs/function/md5";
import config from '@/common/config/config.js'
import dateTool from '@/common/utils/dateTool.js'

function pddSign(list) {
	list.sort();
	console.log(list);
	var key = config.client_secret;
	for (let s of list) {
		key += s;
	}
	key += config.client_secret;
	
	return md5Libs.md5(key).toUpperCase();
}

function generateUrl(params) {
	var timestamp = (dateTool.getCurrentTime() / 1000).toFixed(0);
	if(params.type == 'pdd.ddk.top.goods.list.query'){
		var list = ['type'+params.type, 'data_typeJSON', 'client_id' + config.client_id, 'timestamp' +
			timestamp
		];
		var sign = pddSign(list);
		return 'https://gw-api.pinduoduo.com/api/router?type='+params.type+'&data_type=JSON&client_id=' +
		config.client_id + '&timestamp=' + timestamp + '&sign=' + sign;
	}else if(params.type == 'pdd.ddk.goods.recommend.get'){
		var list = ['type'+params.type, 'data_typeJSON', 'client_id' + config.client_id, 'timestamp' +
			timestamp,'channel_type'+params.channel_type
		];
		var sign = pddSign(list);
		return 'https://gw-api.pinduoduo.com/api/router?type='+params.type+'&data_type=JSON&client_id=' +
		config.client_id + '&channel_type='+params.channel_type+'&timestamp=' + timestamp + '&sign=' + sign;
	}else if(params.type == 'pdd.ddk.goods.search'){
		var keyword = '';
		var opt_id = '';
		var userInfo = uni.getStorageSync('userInfo')
		var custom_parameters = {};
		custom_parameters.uid = userInfo.user_random;
		if(params.hasOwnProperty('keyword')){
			keyword = params.keyword;
		}
		if(params.hasOwnProperty('opt_id')){
			opt_id = params.opt_id;
		}
		var list = ['type'+params.type,'pid'+config.p_id, 'page'+params.page,'page_size'+params.page_size,'data_typeJSON', 'client_id' + config.client_id, 'timestamp' +
			timestamp,'opt_id'+opt_id,'keyword'+keyword,'custom_parameters' + JSON.stringify(
						custom_parameters)
		];
		
		var sign = pddSign(list);
		return 'https://gw-api.pinduoduo.com/api/router?type='+params.type+'&data_type=JSON&client_id=' +
		config.client_id + '&custom_parameters=' +
			JSON.stringify(custom_parameters) +'&page_size='+params.page_size+'&keyword='+keyword+'&page='+params.page+'&opt_id='+opt_id+'&timestamp=' + timestamp +'&pid='+config.p_id+ '&sign=' + sign;
	}else if(params.type == 'pdd.goods.opt.get'){
		var list = ['type'+params.type, 'data_typeJSON', 'client_id' + config.client_id, 'timestamp' +
			timestamp,'parent_opt_id'+params.parent_opt_id
		];
		var sign = pddSign(list);
		return 'https://gw-api.pinduoduo.com/api/router?type='+params.type+'&data_type=JSON&client_id=' +
		config.client_id + '&parent_opt_id='+params.parent_opt_id+'&timestamp=' + timestamp + '&sign=' + sign;
	}
	
	
	
}

export default{
	pddSign,
	generateUrl
}